package modelType

import (
	"database/sql/driver"
	"encoding/json"
)

// JsonObjectType 单对象的json字符串
type JsonObjectType map[string]interface{}

func (receiver JsonObjectType) Value() (driver.Value, error) {
	marshal, err := json.Marshal(receiver)
	if err != nil {
		return "{}", err
	}
	return string(marshal), nil
}

func (receiver *JsonObjectType) Scan(value interface{}) error {
	err := json.Unmarshal(value.([]byte), receiver)
	if err != nil {
		return err
	}
	return nil
}

// JsonArrayType 数组Json对象字符串
type JsonArrayType []map[string]interface{}

func (receiver JsonArrayType) Value() (driver.Value, error) {
	marshal, err := json.Marshal(receiver)
	if err != nil {
		return "{}", err
	}
	return string(marshal), nil
}

func (receiver *JsonArrayType) Scan(value interface{}) error {
	err := json.Unmarshal(value.([]byte), receiver)
	if err != nil {
		return err
	}
	return nil
}
